package com.example.aircraftwar.codes.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.aircraftwar.R;

public class MenuActivity extends AppCompatActivity {

    private long mExitTime = 0;
    private static String difficulty;
    private boolean videoOpen;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        Button simpleButton = (Button) findViewById(R.id.simple);
        simpleButton.setOnClickListener(view -> {
            difficulty="simple";
            startGame();
        });
        Button normalButton = (Button) findViewById(R.id.normal);
        normalButton.setOnClickListener(view -> {
            difficulty="normal";
            startGame();
        });
        Button difficultButton = (Button) findViewById(R.id.difficult);
        difficultButton.setOnClickListener(view -> {
            difficulty="difficult";
            startGame();
        });
        Spinner videoSwitch = (Spinner) findViewById(R.id.videoSwitch);
        videoSwitch.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                videoOpen = ("开".equals(adapterView.getItemAtPosition(i).toString()));
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {}
        });
    }

    private void startGame(){
        Intent intent = new Intent(MenuActivity.this,GameActivity.class);
        intent.putExtra("difficulty",difficulty);
        intent.putExtra("videoOpen",videoOpen);
        Toast.makeText(MenuActivity.this,"音效:"+videoOpen+". 难度:"+difficulty,Toast.LENGTH_SHORT).show();
        finish();
        startActivity(intent);
    }

    @Override
    public void onBackPressed() {
        if(System.currentTimeMillis() - mExitTime > 2000){
            Toast.makeText(MenuActivity.this,"再按一次退出程序",Toast.LENGTH_SHORT).show();
            mExitTime = System.currentTimeMillis();
        } else {
            finish();
            try{
                Thread.sleep(1000);
                System.exit(0);
            } catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    }
}